﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StillVoiceCommon;
using TN5Y;
using System.Data;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;

namespace TeamTime.Classes
{
    public class TN5YProject: DataTableRow

    {
        public string CompanyProject;
        public string CompanyName;
        public new void load(string id)
        {
            base.load(id);
            System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text,
                "select * from ViewTN5YProjects where ID = " + @id,
                new SqlParameter("id", id));
            if (dr.Read())
            {
                CompanyName = dr["CompanyName"].ToString();
                CompanyProject = dr["CompanyProject"].ToString();
           }
            dr.Close();
            dr = null;
        }
       public TN5YProject()
       {
           this.initClass(Utilities.DBConnectionString, "TN5YProject");
       }
    }
    public class TN5YProjects
    {
        internal DataSet search(string searchFor)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM ViewTN5YProjects where 1=1 ");
            if (searchFor != "")
            {
                sql.Append(" and (ProjectName like '%'+@searchFor+'%' or");
                sql.Append(" Notes like '%'+@searchFor+'%' ");

                sql.Append(") ");
            }

            //if (sortExpression != "")
            //{
            //    sql.Append(" order by " + sortExpression);
            //}
            return SqlHelper.ExecuteDataset(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql.ToString(),
                                                new SqlParameter("searchFor", searchFor));
        }



        internal DataSet getProjects(bool anyOption)
        {
            string sql = " select ID, CompanyProject from ViewTN5YProjects order by CompanyProject";
            if (anyOption) sql = "select 0 as ID,'<Any>' as CompanyProject UNION " + sql;
            return SqlHelper.ExecuteDataset(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql);
        }
       
        internal string getProjectNameByID(int ProjectID)
        {
            string sql = "select ProjectName from TN5YProject where ID = @ID";
            return SqlHelper.ExecuteScalar(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
                            new SqlParameter("CompanyID", ProjectID)).ToString();
        }
        public Int32 getProjectIDByName(string projectName)
        {
            Int32 ret = 0;
            string sql = "select ID from TN5YProject where ProjectName = '" + projectName + "' ";
            SqlDataReader dr;
            dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql);
            if (dr.Read())
            {
                ret = Convert.ToInt32(dr[0]);
            }
            dr.Close();
            return ret;
        }

      
    }
}
